package com.htdata.iiot.api.tsdb.pojo;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Map.Entry;

/**
 * Serializer for DataPoint from string
 * @author shanyou
 *
 */
public class DataPointSerializer extends JsonSerializer<DataPoint> {

	@Override
	public void serialize(DataPoint dataPoint, JsonGenerator jgen, SerializerProvider serializers)
			throws IOException, JsonProcessingException {
		jgen.writeStartObject();
		jgen.writeStringField("dn", dataPoint.getDeviceNumber().toBase64String());
		jgen.writeNumberField("ts", dataPoint.getTs());
		
		jgen.writeObjectFieldStart("tags");
        for (Entry<String, String> tag : dataPoint.getTags().entrySet()) {
            jgen.writeStringField(tag.getKey(), tag.getValue());
        }
        jgen.writeEndObject();
        
        jgen.writeStringField("metric", dataPoint.getMetric().getName());
        jgen.writeNumberField("value", dataPoint.getMetric().getValue());
        jgen.writeEndObject();
        
        
	}

}
